<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNum">
        <el-input
            v-model="queryParams.orderNum"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务类型" prop="stId">
        <el-input
            v-model="queryParams.stId"
            placeholder="请选择服务类型"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="orderTime">
        <el-date-picker clearable
                        v-model="queryParams.orderTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预约时间" prop="appointTime">
        <el-date-picker clearable
                        v-model="queryParams.appointTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input
            v-model="queryParams.contact"
            placeholder="请输入联系方式"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select style="width: 200px" v-model="queryParams.orderStatus" placeholder="全部" clearable>
          <el-option
              v-for="dict in sys_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['order:orders:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['order:orders:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['order:orders:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['order:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="订单ID" align="center" prop="oId" />-->
      <el-table-column label="订单编号" align="center" prop="orderNum" />
      <!--      <el-table-column label="服务ID" align="center" prop="smId" />-->
      <el-table-column label="服务编号" align="center" prop="smId" />
      <!--      <el-table-column label="服务类型ID" align="center" prop="stId" />-->
      <el-table-column label="服务类型" align="center" prop="stId" />
      <!--      <el-table-column label="用户ID" align="center" prop="uId" />-->
      <el-table-column label="车主姓名" align="center" prop="ownerName" />
      <el-table-column label="联系方式" align="center" prop="contact" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="商家ID" align="center" prop="mId" />
      <el-table-column label="车辆ID" align="center" prop="viId" />
      <!--      <el-table-column label="优惠券ID" align="center" prop="cId" />-->
      <el-table-column label="价格" align="center" prop="payPrice" />
      <el-table-column label="预约时间" align="center" prop="appointTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.appointTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="核销码" align="center" prop="verifyCode" />-->
      <!--      <el-table-column label="核销时间" align="center" prop="verifyTime" width="180">-->
      <!--        <template #default="scope">-->
      <!--          <span>{{ parseTime(scope.row.verifyTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <dict-tag :options="sys_order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="维修类型ID" align="center" prop="rtId" />-->
      <!--      <el-table-column label="维修部位图片" align="center" prop="repairPhotos" width="100">-->
      <!--        <template #default="scope">-->
      <!--          <image-preview :src="scope.row.repairPhotos" :width="50" :height="50"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="取车位置" align="center" prop="pickLocation" />-->
      <!--      <el-table-column label="逻辑删除" align="center" prop="isDeleted" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['order:orders:edit']">详情</el-button>
          <!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:orders:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="ordersRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="服务ID" prop="smId">
          <el-input v-model="form.smId" placeholder="请输入服务ID" />
        </el-form-item>
        <el-form-item label="服务类型ID" prop="stId">
          <el-input v-model="form.stId" placeholder="请输入服务类型ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="uId">
          <el-input v-model="form.uId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="车主姓名" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入车主姓名" />
        </el-form-item>
        <el-form-item label="商家ID" prop="mId">
          <el-input v-model="form.mId" placeholder="请输入商家ID" />
        </el-form-item>
        <el-form-item label="车辆ID" prop="viId">
          <el-input v-model="form.viId" placeholder="请输入车辆ID" />
        </el-form-item>
        <el-form-item label="优惠券ID" prop="cId">
          <el-input v-model="form.cId" placeholder="请输入优惠券ID" />
        </el-form-item>
        <el-form-item label="价格" prop="payPrice">
          <el-input v-model="form.payPrice" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="下单时间" prop="orderTime">
          <el-date-picker clearable
                          v-model="form.orderTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约时间" prop="appointTime">
          <el-date-picker clearable
                          v-model="form.appointTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="核销码" prop="verifyCode">
          <el-input v-model="form.verifyCode" placeholder="请输入核销码" />
        </el-form-item>
        <el-form-item label="核销时间" prop="verifyTime">
          <el-date-picker clearable
                          v-model="form.verifyTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择核销时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
            <el-option
                v-for="dict in sys_order_status"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修类型ID" prop="rtId">
          <el-input v-model="form.rtId" placeholder="请输入维修类型ID" />
        </el-form-item>
        <el-form-item label="维修部位图片" prop="repairPhotos">
          <image-upload v-model="form.repairPhotos"/>
        </el-form-item>
        <el-form-item label="取车位置" prop="pickLocation">
          <el-input v-model="form.pickLocation" placeholder="请输入取车位置" />
        </el-form-item>
        <el-form-item label="逻辑删除" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入逻辑删除" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Orders">
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/order/orders"

const { proxy } = getCurrentInstance()
const { sys_order_status } = proxy.useDict('sys_order_status')

const ordersList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNum: null,
    stId: null,
    orderTime: null,
    appointTime: null,
    contact: null,
    orderStatus: null,
  },
  rules: {
    orderNum: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ],
    smId: [
      { required: true, message: "服务ID不能为空", trigger: "blur" }
    ],
    stId: [
      { required: true, message: "服务类型ID不能为空", trigger: "blur" }
    ],
    uId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    ownerName: [
      { required: true, message: "车主姓名不能为空", trigger: "blur" }
    ],
    mId: [
      { required: true, message: "商家ID不能为空", trigger: "blur" }
    ],
    viId: [
      { required: true, message: "车辆ID不能为空", trigger: "blur" }
    ],
    payPrice: [
      { required: true, message: "价格不能为空", trigger: "blur" }
    ],
    orderTime: [
      { required: true, message: "下单时间不能为空", trigger: "blur" }
    ],
    appointTime: [
      { required: true, message: "预约时间不能为空", trigger: "blur" }
    ],
    verifyCode: [
      { required: true, message: "核销码不能为空", trigger: "blur" }
    ],
    contact: [
      { required: true, message: "联系方式不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询订单信息列表 */
function getList() {
  loading.value = true
  listOrders(queryParams.value).then(response => {
    ordersList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    oId: null,
    orderNum: null,
    smId: null,
    stId: null,
    uId: null,
    ownerName: null,
    mId: null,
    viId: null,
    cId: null,
    payPrice: null,
    orderTime: null,
    appointTime: null,
    verifyCode: null,
    verifyTime: null,
    contact: null,
    remark: null,
    orderStatus: null,
    rtId: null,
    repairPhotos: null,
    pickLocation: null,
    isDeleted: null
  }
  proxy.resetForm("ordersRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.oId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加订单信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // reset()
  // const _oId = row.oId || ids.value
  // getOrders(_oId).then(response => {
  //   form.value = response.data
  //   open.value = true
  //   title.value = "修改订单信息"
  // })
  console.log("点击了详情按钮")
  console.log(row.oid)
  router.push({
    path: '/order/detail',
    query: {
      oId: row.oid
    }
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["ordersRef"].validate(valid => {
    if (valid) {
      if (form.value.oId != null) {
        updateOrders(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addOrders(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _oIds = row.oId || ids.value
  proxy.$modal.confirm('是否确认删除订单信息编号为"' + _oIds + '"的数据项？').then(function() {
    return delOrders(_oIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/orders/export', {
    ...queryParams.value
  }, `orders_${new Date().getTime()}.xlsx`)
}

getList()
</script>
